package com.astar.service.impl;

import com.astar.entity.*;
import com.astar.mapper.UserMapper;
import com.astar.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-01-17
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public Set<String> selectPerms(String loginName) {
        List<Menu> users = userMapper.selectMenuFromLoginName(loginName);
        Set<String> perms = new HashSet<>();
        users.forEach(e->{
            perms.add(e.getPerms());
        });
        return perms;
    }

    @Override
    public List<CustomUser> selectUserPower(String loginName) {
        return userMapper.selectUserPower(loginName);
    }

    @Override
    public Role selectAllForId(Integer id) {
        return userMapper.selectAllForId(id);
    }

    @Override
    public List<CustomUser> selectAll(Integer id) {
        return userMapper.selectAll(id);
    }

    @Override
    public List<Dept> selectDepTree() {
        return userMapper.selectDepTree();
    }

    @Override
    public List<User> select() {
        return userMapper.select();
    }

    @Override
    public List<CusUser> queryUserByDeptId(Integer deptId, String userName, String loginName) {
        CusUser cusUser = new CusUser();
        cusUser.setLoginName(loginName);
        cusUser.setUserName(userName);
        if(deptId==null || deptId==100){
            cusUser.setDeptId(null);
        }else{
            cusUser.setDeptId(deptId);
        }
        return userMapper.queryUserByDeptId(cusUser);
    }

    @Override
    public CusUser selectForid(Integer id) {
        return userMapper.selectForId(id);
    }

    @Override
    public int updateForId(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int delete(User user) {
        user.setStatus("1");
        return userMapper.updateById(user);
    }
}
